{% extends 'myadmin/base.html' %}
{% block main_body %}
<style>
    /* 紧凑布局核心样式 */
    .form-card {
        background: #fff;
        border-radius: 6px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        padding: 20px;
    }
    .form-section {
        margin-bottom: 20px;
        padding-bottom: 15px;
        border-bottom: 1px solid #f1f1f1;
    }
    .form-section:last-child {
        border-bottom: none;
        margin-bottom: 0;
        padding-bottom: 0;
    }
    .form-section h4 {
        margin: 0 0 15px;
        font-size: 16px;
        color: #3c8dbc;
        display: flex;
        align-items: center;
    }
    .form-section h4 i {
        margin-right: 8px;
    }
    .form-row {
        display: flex;
        flex-wrap: wrap;
        margin: 0 -10px; /* 抵消列的内边距 */
    }
    .form-col {
        flex: 1;
        min-width: 200px; /* 缩小最小宽度以容纳更多字段 */
        padding: 0 10px;
        box-sizing: border-box;
    }
    .form-group {
        margin-bottom: 12px;
        width: 100%;
    }
    .form-group label {
        font-weight: 600;
        font-size: 13px;
        margin-bottom: 5px;
        display: block;
        width: 100%;
    }
    .form-control {
        border-radius: 4px;
        padding: 6px 10px;
        font-size: 13px;
        height: 34px;
        width: 100%;
        box-sizing: border-box;
    }
    textarea.form-control {
        height: auto;
        min-height: 60px;
    }
    .help-text {
        font-size: 12px;
        color: #666;
        margin-top: 3px;
        display: block;
    }
    .required-mark {
        color: #dd4b39;
        margin-left: 3px;
    }
    .btn-group {
        margin-top: 15px;
        display: flex;
        gap: 10px;
        justify-content: flex-end;
        padding: 0 10px;
    }
    .btn {
        padding: 6px 15px;
        font-size: 13px;
    }
    .toggle-content {
        transition: all 0.3s ease;
    }
</style>

<script>
    // 页面加载完成后初始化
    document.addEventListener('DOMContentLoaded', function() {
        // 自身免疫性疾病显示控制
        const hasAutoimmuneSelect = document.querySelector('select[name="has_autoimmune_disease"]');
        const autoimmuneTypeGroup = document.getElementById('autoimmune_diseaseType');
        
        // 糖尿病显示控制
        const hasDiabetesSelect = document.querySelector('select[name="has_diabetes"]');
        const diabetesTypeGroup = document.getElementById('diabetesTypeGroup');
        
        // 初始化显示状态
        updateAutoimmuneTypeVisibility();
        updateDiabetesTypeVisibility();
        
        // 事件监听
        hasAutoimmuneSelect.addEventListener('change', updateAutoimmuneTypeVisibility);
        hasDiabetesSelect.addEventListener('change', updateDiabetesTypeVisibility);
        
        // 自身免疫性疾病显示控制函数
        function updateAutoimmuneTypeVisibility() {
            if (hasAutoimmuneSelect.value === 'True') {
                autoimmuneTypeGroup.style.display = 'block';
                autoimmuneTypeGroup.style.opacity = '1';
                autoimmuneTypeGroup.style.height = 'auto';
                autoimmuneTypeGroup.style.overflow = 'visible';
            } else {
                autoimmuneTypeGroup.style.display = 'none';
                autoimmuneTypeGroup.style.opacity = '0';
                autoimmuneTypeGroup.style.height = '0';
                autoimmuneTypeGroup.style.overflow = 'hidden';
            }
        }
        
        // 糖尿病显示控制函数
        function updateDiabetesTypeVisibility() {
            if (hasDiabetesSelect.value === 'True') {
                diabetesTypeGroup.style.display = 'block';
                diabetesTypeGroup.style.opacity = '1';
            } else {
                diabetesTypeGroup.style.display = 'none';
                diabetesTypeGroup.style.opacity = '0';
            }
        }
    });
</script>

<section class="content-header">
    <h1>健康数据管理 <small>编辑健康记录</small></h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-home"></i> 首页</a></li>
        <li><a href="#">健康数据管理</a></li>
        <li class="active">编辑健康记录</li>
    </ol>
</section>

<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">健康记录编辑</h3>
                </div>
                <form method="post" action="{% url 'myadmin_userbodyrecords_edit' ubr.record_id %}" class="form-card">
                    {% csrf_token %}
                    
                    <!-- 1. 基础记录信息 -->
                    <div class="form-section">
                        <h4><i class="fa fa-list-alt"></i> 基础记录信息</h4>
                        <div class="form-row">
                            <div class="form-col">
                                <div class="form-group">
                                    <label>记录ID</label>
                                    <input type="text" value="{{ ubr.record_id }}" class="form-control" disabled>
                                    <span class="help-text">系统自动生成</span>
                                </div>
                            </div>
                            <div class="form-col">
                                <div class="form-group">
                                    <label>用户ID</label>
                                    <input type="text" value="{{ ubr.user_id }}" class="form-control" disabled>
                                </div>
                            </div>
                            <div class="form-col">
                                <div class="form-group">
                                    <label>用户名</label>
                                    <input type="text" value="{{ ubr.user.username|default:'未知用户' }}" class="form-control" disabled>
                                </div>
                            </div>
                            <div class="form-col">
                                <div class="form-group">
                                    <label>记录日期 <span class="required-mark">*</span></label>
                                    <input type="date" name="record_date" value="{{ ubr.record_date|date:'Y-m-d' }}" class="form-control" required>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 2. 身体成分信息 -->
                    <div class="form-section">
                        <h4><i class="fa fa-balance-scale"></i> 身体成分信息</h4>
                        <div class="form-row">
                            <div class="form-col">
                                <div class="form-group">
                                    <label>体重(kg)</label>
                                    <input type="number" name="weight" value="{{ ubr.weight|default:'' }}" class="form-control" min="10" max="300" step="0.1">
                                </div>
                            </div>
                            <div class="form-col">
                                <div class="form-group">
                                    <label>体脂率(%)</label>
                                    <input type="number" name="body_fat_percentage" value="{{ ubr.body_fat_percentage|default:'' }}" class="form-control" min="0" max="100" step="0.1">
                                </div>
                            </div>
                            <div class="form-col">
                                <div class="form-group">
                                    <label>肌肉量(kg)</label>
                                    <input type="number" name="muscle_mass" value="{{ ubr.muscle_mass|default:'' }}" class="form-control" min="0" max="200" step="0.1">
                                </div>
                            </div>
                            <div class="form-col">
                                <div class="form-group">
                                    <label>水分率(%)</label>
                                    <input type="number" name="water_percentage" value="{{ ubr.water_percentage|default:'' }}" class="form-control" min="0" max="100" step="0.1">
                                </div>
                            </div>
                            <div class="form-col">
                                <div class="form-group">
                                    <label>骨量(kg)</label>
                                    <input type="number" name="bone_mass" value="{{ ubr.bone_mass|default:'' }}" class="form-control" min="0" max="30" step="0.1">
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 3. 生理指标信息 -->
                    <div class="form-section">
                        <h4><i class="fa fa-heartbeat"></i> 生理指标信息</h4>
                        <div class="form-row">
                            <div class="form-col">
                                <div class="form-group">
                                    <label>收缩压(mmHg)</label>
                                    <input type="number" name="blood_pressure_systolic" value="{{ ubr.blood_pressure_systolic|default:'' }}" class="form-control" min="50" max="200">
                                </div>
                            </div>
                            <div class="form-col">
                                <div class="form-group">
                                    <label>舒张压(mmHg)</label>
                                    <input type="number" name="blood_pressure_diastolic" value="{{ ubr.blood_pressure_diastolic|default:'' }}" class="form-control" min="30" max="120">
                                </div>
                            </div>
                            <div class="form-col">
                                <div class="form-group">
                                    <label>血糖(mmol/L)</label>
                                    <input type="number" name="blood_sugar" value="{{ ubr.blood_sugar|default:'' }}" class="form-control" min="2" max="20" step="0.1">
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 4. 备注信息 -->
                    <div class="form-section">
                        <h4><i class="fa fa-sticky-note"></i> 备注信息</h4>
                        <div class="form-row">
                            <div class="form-col" style="min-width: 100%;">
                                <div class="form-group">
                                    <label>备注内容</label>
                                    <textarea name="notes" class="form-control" rows="3" placeholder="请输入备注信息">{{ ubr.notes|default:'' }}</textarea>
                                    <span class="help-text">最多输入200个字符</span>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 操作按钮区 -->
                    <div class="btn-group">
                        <button type="button" class="btn btn-default" onclick="window.history.back();">
                            <i class="fa fa-arrow-left"></i> 返回
                        </button>
                        <button type="reset" class="btn btn-default">
                            <i class="fa fa-refresh"></i> 重置
                        </button>
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-check"></i> 保存记录
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>
{% endblock %}